package yxy.cherry.paycenter.channelmodule.sdk_vivo_quick;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
import yxy.apple.logger.Logger;
import yxy.apple.util.MD5Utility;
import yxy.apple.util.MathUtil;
import yxy.apple.util.SdkUtil;
import yxy.cherry.data.bean.charge;
import yxy.cherry.game.base.bean.SDKParm;
import yxy.cherry.game.base.bean.SdkParmEnum;
import yxy.cherry.paycenter.channelmodule.PayMoudle_channel;
import yxy.cherry.paycenter.database.db.orders;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.channel.SdkChannel;
import yxy.game.pm2.bean.db.sdk.SDK_UnionChannel;
import yxy.game.pm2.bean.db.sdk.SDK_UnionChannelDAO;
import yxy.game.pm2.bean.module.OrderInfoModule;
import yxy.game.pm2.bean.order.OrderInfo;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.*;

//客户端下单（创建订单）
public class PayMoudle_vivo_quick extends PayMoudle_channel {
	static final Logger logger = Logger.create(PayMoudle_vivo_quick.class);

	@Override
	public String getChannel() {
		return SdkChannel.sdk_vivo_quick;
	}
	@Override
	public JSONObject dealPay(JSONObject infoobj) {
		// TODO Auto-generated method stub
		return null;
	}

	//构造订单信息，生成签名，返回给客户端
	@Override
	public JSONObject createOrderInfo(JSONObject infoobj, orders o) {
		JSONObject json = new JSONObject();
//		String orderNo, String ProductName, double Price, String ProductId
		json.put("orderNo", o.getMyorderid());
		json.put("ProductName", o.getGoodsname());
		json.put("ProductId", o.getGoodsid());
		json.put("Price", o.getGoodsprice());

		//读取渠道配置
		SDK_UnionChannel unionChannel = SDK_UnionChannelDAO.getByChannel(o.getClusterid(), o.getChannel());

		//生成sign
		Map<String, String> params = new HashMap<>();
		params.put("appId", unionChannel.getAppId());
		params.put("cpOrderNumber", o.getMyorderid()); //我方订单号
		params.put("productName", o.getGoodsname());
		params.put("productDesc", o.getGoodsname());
		params.put("orderAmount", String.valueOf(o.getGoodsprice())); //分
		params.put("notifyUrl", unionChannel.getNotifyUrl());
		params.put("expireTime", LocalDateTime.now().plusDays(1).toString("yyyyMMddHHmmss"));
		params.put("extInfo", "");
		logger.info(SdkChannel.sdk_vivo_quick + ":createOrderInfo : %s", JSON.toJSON(params));
		String content = SdkUtil.mapToContent(params, false);
		content += "&" + MD5Utility.MD5(unionChannel.getPrivateKey()).toLowerCase();
		logger.info(SdkChannel.sdk_vivo_quick + ":createOrderInfo:content:%s", content);
		String signature = MD5Utility.MD5(content).toLowerCase();

		params.put("vivoSignature", signature);
		json.put("orderInfo", params);
		logger.info(SdkChannel.sdk_vivo_quick + ":createOrderInfo : %s", JSON.toJSON(json));
		
		return json;
	}

	//创建订单
	public orders createOrderVIVOQUICK(int dataid, String playercode, int serverid, int acttype) {
		orders o = createOrder(getChannel(), dataid, acttype, playercode, serverid);
		addOrder(o);
		return o;
	}
	
	/**
	 * 创建 OrderInfo
	 * @param o
	 * @param curs
	 * @param c
	 * @param payamount
	 */
	public void createOrderInfoModule(orders o, ArrayList<Currency> curs, charge c, int payamount) {
		createOrderInfoModule(o, curs, c.getVipexp(), payamount);
	}
	
	/**
	 * 
	 * @param o
	 * @param curs
	 * @param vipexp
	 * @return
	 */
	public void createOrderInfoModule(orders o, ArrayList<Currency> curs, int vipexp, int payamount) {
		OrderInfo oi = OrderInfoModule.createOrderInfo(o.getGoodsid(), o.getChannel(), o.getMyorderid(), new DateTime(o.getCreatetime().getTime()), 
				OrderInfoModule.orderinforuning, o.getPlatorderid(), o.getActtype(), o.getPlayercode(), curs, 
				o.getServerid(), DateTime.now(), vipexp, payamount, o.getGoodsprice());
		logger.info("createOrderInfoModule %s", JSON.toJSONString(oi));
		OrderInfo.set(o.getPlayercode(), oi);
	}
}
